// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import VueRouter from 'vue-router'
import App from './App.vue'
import baseRoutes from './router/base'
import Route from './router/index'
import store from './store'
import Antd from 'ant-design-vue'
import 'viewerjs/dist/viewer.css'
import VueViewer from 'v-viewer'

// css样式文件
import './assets/css/export.scss'
import 'ant-design-vue/dist/antd.css'

// 百度文本编辑器
import '../public/ueditor/ueditor.config.js'
import '../public/ueditor/ueditor.all.js'
import '../public/ueditor/lang/zh-cn/zh-cn.js'

import Tools from './util/tools'
import Util from './util/util'

Vue.config.productionTip = false

const originalPush: any = VueRouter.prototype.push
VueRouter.prototype.push = function push (location: any) {
  return originalPush.call(this, location).catch((err: any) => err)
}

Vue.use(Antd)
// 初始化路由
Vue.use(VueRouter)
Vue.use(VueViewer)
Route.routes = baseRoutes
const routes = Route.initRoutes()
export const router = new VueRouter({routes})

// 配置tools
export const headerConfig = {
  Appkey: '67167560c2424f2c92kxkw3c0afhuang',
  AppSecret: 'e8c410df2f433a92jmfjaemysehuangx'
}
Tools({Vue, headerConfig})

// 百度编辑器上传图片地址
localStorage.setItem('uploadUrl', Util.uploadURL());

// 路由跳转前
router.beforeEach((to: any, from, next) => {
  const account = localStorage.getItem('account')
  const password = localStorage.getItem('password')
  const sessionId = localStorage.getItem('sessionId')
  const path = localStorage.getItem('path')
  const code = to.query.code || Vue.prototype.$util.getQueryString('code')

  if (to.path !== '/' && to.path !== '/login') {
    localStorage.setItem('path', to.fullPath)
  }

  if (to.path !== '/' && !sessionId && to.path !== '/login') {
    localStorage.setItem('path', to.fullPath)
    return next('/')
  }
    next()
})

/* eslint-disable no-new */
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
